import numpy as np
import pandas as pd

path = "./data/ratings.csv"

data = pd.read_csv(path)

print('data.size:', data.size, "data.shape:", data.shape, "data.dtypes:\n", data.dtypes)
print('data.columns:', data.columns)
print('data.describe:\n', data.describe())

print('=-------------data-----------=')
print(data)

print('1.=-------------查询第二行数据-----------=')
df1 = data.iloc[1:2, :]
print(df1)

print('2.=-------------查询头三行rating-----------=')
# df2 = data.iloc[0:3, :]
# df2 = df2.loc[:, ['rating']]

df2 = data.loc[0:2, ['rating']]
print(df2)

print('3.=-------------查询所有rating高于4的-----------=')
df3 = data.loc[(data["rating"] > 4), :]
print(df3)

print('4.=-------------查询所有rating在1&3的数据-----------=')
# df4 = data.loc[(data["rating"] >= 1), :]
# df4 = data.loc[(data["rating"] <= 3), :]
df4 = data.loc[(data["rating"] >= 1) & (data["rating"] <= 3), :]
print(df4)

print('5.=-------------统计rating各分段直方图-----------=')
df4 = data.loc[:, ["rating"]]
print(data['rating'].value_counts())

'''
数据预处理
1.rating有缺失的，直接删除
2.time有缺失值，用出现最多的进行填充
'''
print('--------------------------------------------------------')
'''
删除rating出现NAN的行
'''
data.dropna(subset=['rating'], inplace=True)

print('---------------- 删除 rating 为空的列----------------------')
print(data)
# df = data.loc[(data["rating"] is ) & (data["rating"] <= 3), :]

'''
 求直方图中出现次数最大的值
'''
max_times_value = data['time'].value_counts().idxmax();

'''
对time这行的NAN值使用max_times替换
'''
data["time"].fillna(value=max_times_value,
                    method=None,
                    axis=None,
                    inplace=True,
                    limit=None,
                    downcast=None, )
print('---------------- 填充time 空列----------------------')
print(data)
